﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Random = UnityEngine.Random;

public class goods : MonoBehaviour {
    private Animator animator;
    private float[] randvalue = { 0.5f, 0.25f, 0.2f, 0.05f };
    public GameObject s;
    private bool top;
    private bool left;
    private bool bottom;
    private bool right;
    public Text txt;
    public AI ai;
    public Image img;
	// Use this for initialization
	void Start () {
        animator = GetComponent<Animator>();
        s.GetComponent<lqf_move>().AI_Move += Listener;
        ai = new AI();
	}
    void Listener(bool bool_top,bool bool_left,bool bool_bottom,bool bool_right)
    {
        top = bool_top;
        left = bool_left;
        bottom = bool_bottom;
        right = bool_right;
    }
	// Update is called once per frame
	void Update () {
        AnimatorStateInfo StateInfo = animator.GetCurrentAnimatorStateInfo(0);
        int random_value_1 = Random.Range(1, 100);
        if (random_value_1 >= 1 && random_value_1 < 25)
        {
            animator.SetBool("top", true);
            animator.SetBool("left", false);
            animator.SetBool("bottom", false);
            animator.SetBool("right", false);
        }
        if (random_value_1 >= 25 && random_value_1 < 50)
        {
            animator.SetBool("top", false);
            animator.SetBool("left", true);
            animator.SetBool("bottom", false);
            animator.SetBool("right", false);
        }
        if (random_value_1 >= 50 && random_value_1 < 75)
        {
            animator.SetBool("top", false);
            animator.SetBool("left", false);
            animator.SetBool("bottom", true);
            animator.SetBool("right", false);
        }
        if (random_value_1 >= 75 && random_value_1 < 100)
        {
            animator.SetBool("top", false);
            animator.SetBool("left", false);
            animator.SetBool("bottom", false);
            animator.SetBool("right", true);
        }
	}

    int Random_AI(float[] ai_random)
    {
        float total = 0.0f;
        foreach(float elem in ai_random){
            total += elem;
        }
        float ramdomPoint = Random.value * total;
        int i;
        for (i = 0; i < ai_random.Length; i++)
        {
            if (ramdomPoint < ai_random[i])
            {
                return i;
            }
            else
            {
                ramdomPoint -= ai_random[i];
            }
        }
        return ai_random.Length - 1;
    }

    void OnAnimatorMove()
    {
        AnimatorStateInfo StateInfo = animator.GetCurrentAnimatorStateInfo(0);
        if (animator.GetBool("top") && !StateInfo.IsName("Base Layer.goods_idea"))
        {

            Vector2 v2 = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y + 0.16f * 5 * Time.deltaTime);
            transform.position = v2;
        }
        if (animator.GetBool("bottom") && !StateInfo.IsName("Base Layer.goods_idea"))
        {
            Vector2 v2 = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.16f * 5 * Time.deltaTime);
            transform.position = v2;
        }
        if (animator.GetBool("left") && !StateInfo.IsName("Base Layer.goods_idea"))
        {
            Vector2 v2 = new Vector2(gameObject.transform.position.x - 0.16f * 5 * Time.deltaTime, gameObject.transform.position.y);
            transform.position = v2;
        }
        if (animator.GetBool("right") && !StateInfo.IsName("Base Layer.goods_idea"))
        {
            Vector2 v2 = new Vector2(gameObject.transform.position.x + 0.16f * 5 * Time.deltaTime, gameObject.transform.position.y);
            transform.position = v2;
        }
    }


    void OnCollisionEnter2D(Collision2D col)
    {


        if (col.gameObject.name == "lqf_1")
        {

            int random_value = Random_AI(randvalue);
            img.gameObject.SetActive(true);
            switch (random_value)
            {
                case 0: txt.text = ai.goods_role[0]; break;
                case 1: txt.text = ai.goods_role[1]; break;
                case 2: txt.text = ai.goods_role[2]; break;
                case 3: txt.text = ai.goods_role[3]; break;
                default: txt.text = ai.goods_role[4]; break;
            }

            if (top)
            {
                animator.SetBool("idea_bottom", true);
                animator.SetBool("idea_left", false);
                animator.SetBool("idea_top", false);
                animator.SetBool("idea_right", false);
                
            }
            if (left)
            {
                animator.SetBool("idea_bottom", false);
                animator.SetBool("idea_left", false);
                animator.SetBool("idea_top", false);
                animator.SetBool("idea_right", true);
            }
            if (bottom)
            {
                animator.SetBool("idea_bottom", false);
                animator.SetBool("idea_left", false);
                animator.SetBool("idea_top", true);
                animator.SetBool("idea_right", false);
            }
            if (right)
            {
                animator.SetBool("idea_bottom", false);
                animator.SetBool("idea_left", true);
                animator.SetBool("idea_top", false);
                animator.SetBool("idea_right", false);
            }

        }
    }
}
